<template>
  <h1>订单管理</h1>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="订单id" prop="orderNo" />
    <el-table-column label="收货人手机号" prop="linkPhone" />
    <el-table-column prop="state" label="是否发货" width="150">
      <template v-slot="scope">
        {{ isFahuo[scope.row.state] }}
      </template>
    </el-table-column>
    <el-table-column label="收获地址" prop="linkAddress" />
    <el-table-column label="创建时间" prop="createTime" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button size="large" @click="delivery(scope.$index, scope.row)"
          >发货</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 删除弹框 -->
  <el-dialog
    v-model="centerDialogVisible"
    title="Warning"
    width="30%"
    align-center
  >
    <span>你确定要删除{{ orderInfo }}的内容么？</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { computed, onMounted, ref } from "vue";

import { getOrderApi, getDeliveryApi } from "../api/order.js";

interface User {
  date: string;
  name: string;
  address: string;
}
const isFahuo = ["未支付", "已支付", "待收货", "待评论"];
// 定义ref
const myOrderList = ref();
const isEmpty = ref(0);

// 删除弹框
const centerDialogVisible = ref<boolean>(false);
const orderInfo = ref();
const search = ref("");
const filterTableData = computed(() =>
  myOrderList.value?.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
);
const handleEdit = (index: number, row: User) => {
  console.log(index, row);
};
const handleDelete = (index: number, row: User) => {
  orderInfo.value = row?.name;
  centerDialogVisible.value = true;
  console.log(orderInfo.value);
};
// 获取我的订单列表
const orderList = async () => {
  const res = await getOrderApi();
  myOrderList.value = res.data.data;
  myOrderList.value > 0 ? (isEmpty.value = 1) : (isEmpty.value = 0);
};

// 发货按钮
const delivery = async (index, value) => {
  const res = await getDeliveryApi(value.id);
  if (res.data.code == "200") {
    ElMessage({
      message: "发货成功",
      type: "success",
    });
  } else {
    ElMessage({
      message: "没给钱发什么货",
      type: "error",
    });
  }
  console.log(res);
};
onMounted(() => {
  orderList();
});
</script>

<style scoped></style>
